const path = require('path')

const Koa = require('koa')

const koaStatic = require('koa-static')

const { koaBody } = require('koa-body')

const error = require('koa-json-error')

const parameter = require('koa-parameter')

require('./dbs')

const app = new Koa()

const getRoutes = require('./routes')

app.use(koaStatic(path.join(__dirname, 'public')))

app.use(
  error({
    postFormat: (e, { stack, ...rest }) =>
      // NOTE: 设置 error 的堆栈信息，在生产环境中不展示
      process.env.NODE_ENV === 'production' ? rest : { stack, ...rest }
  })
)

// NOTE: https://github.com/koajs/koa-body#readme
app.use(
  koaBody({
    multipart: true,
    formidable: {
      uploadDir: path.join(__dirname, '/public/uploads/'),
      keepExtensions: true
    }
  })
)

app.use(parameter(app))

getRoutes(app)

app.listen(3000, () => {
  console.log('the program started at 3000 port')
})
